<template>
  <el-card class="box-card">
    <el-form :model="editForm" ref="editForm">
      <el-form-item label="收货人姓名" prop="userName" :rules="{required: true, message: '请输入收货人姓名', trigger: 'blur'}">
        <el-input v-model="editForm.userName" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="收货人电话" prop="userPhone" :rules="[{required: true, message: '请输入收货人电话', trigger: 'blur'}]">
        <el-input v-model="editForm.userPhone"></el-input>
      </el-form-item>
      <el-form-item label="收货地址" prop="address" :rules="[{required: true, message: '请输入收货地址', trigger: 'blur'}]">
        <el-input v-model="editForm.address"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <el-input v-model="editForm.description"></el-input>
      </el-form-item>
    </el-form>
    <el-button @click="update()">确定</el-button>
    <el-button @click="back()">返回</el-button>
  </el-card>
</template>
<style>
</style>
<script>
export default{
  created() {
     this.loadGridData();
  },
  data(){
     return{
        editForm:{
           userName:'',
           userPhone:'',
           address:'',
           description:''
        },
     }
  },
  methods:{
     loadGridData() {
        let receiverId=this.$route.params.receiverId;
        this.isNewPage = receiverId === undefined
        if(this.isNewPage){
          return
        }else{
        let receiverId=this.$route.params.receiverId
			  this.axios.get(`users/receivers/${receiverId}`).then(response=>{
			    this.editForm=response.data})
			  }
     },
     back(){
        let id=this.$route.params.id
        this.$router.push(`/users/${id}/receivers`)
     },
     update(){
        let receiverId=this.$route.params.receiverId
        this.$refs.editForm.validate((valid) => {
					if (!valid) {
						 return
					}
				  this.$confirm('确认提交吗？','提示',{
				    cancelButtonText: '取消',
            confirmButtonText: '确定',
				  }).then(() => {
				    if(this.isNewPage){
				       let userId=this.$route.params.id
				       this.axios.post(`users/${userId}/receivers`,this.editForm)
				       .then(()=>{
                this.$notify({
                  title: '成功',
                  message: '提交成功',
                  type: 'success'
                })
              }).then(()=>{this.back()})
            }
            else{
              this.axios.put(`users/receivers/${receiverId}`,this.editForm)
              .then(()=>{
                this.$notify({
                  title: '成功',
                  message: '提交成功',
                  type: 'success'
                 })
              }).then(()=>{this.back()})
            }
           })
        })
     }
  }
}

</script>
